﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“guestDataSet.G_problemType”中。您可以根据需要移动或删除它。
            Sxcd();
            this.StartPosition = FormStartPosition.CenterScreen;
            string path = System.Environment.CurrentDirectory + @"\html\";
            if (Directory.Exists(path) != false)
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                FileInfo[] files = dir.GetFiles();
                关于ToolStripMenuItem.Text = "清空冗余文件(" + files.Length.ToString() + ")";
            }

        }
       
        #region 代码
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            xuanz();
        }

        public void xuanz()
        {
            if (listBox1.SelectedValue != null)
            {
                string xuan = listBox1.SelectedValue.ToString();
                get(xuan);
            }
        }

        private void get(string vald)
        {

            Mydb mydb = new Mydb();

            DataTable dt = mydb.getDT("G_id,G_Name", "G_problem", "where G_Type='" + vald + "' order by G_id");

            listBox2.Items.Clear();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                listBox2.Items.Insert(i, dt.Rows[i]["G_id"].ToString() + "—" + dt.Rows[i]["G_Name"].ToString());
            }

            mydb.coleconn();

        }

        private void listBox2_DoubleClick(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem != null)
            {
                string name = listBox2.SelectedItem.ToString();
                string[] nameStr = name.Split('—');
                Form5 Form5 = new Form5(nameStr[0]);
                Form5.Text = nameStr[1];
                Form5.Show();
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            suoyou();
        }

        public void suoyou()
        {
            this.g_problemTypeTableAdapter.Fill(this.guestDataSet.G_problemType);
        }

        public void Sxcd()
        {
            //this.g_problemTypeTableAdapter.Fill(this.guestDataSet.G_problemType);
            this.g_problemTypeTableAdapter.FillBy1(this.guestDataSet.G_problemType);
        }

        private void 修改代码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem != null)
            {
                string name = listBox2.SelectedItem.ToString();
                string[] nameStr = name.Split('—');
                FormTextEdit Form3 = new FormTextEdit(nameStr[0]);
                Form3.ShowDialog();
                xuanz();
            }
        }

        private void 添加代码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormTextEdit FormTextEdit = new FormTextEdit("");
            FormTextEdit.ShowDialog();
        }

        private void 添加类型ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form4 form4 = new Form4("");
            form4.ShowDialog();
            Sxcd();
        }

        private void 修改类型ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                string name = listBox1.SelectedValue.ToString();
                Form4 form4 = new Form4(name);
                form4.ShowDialog();
                Sxcd();
            }
        }

        private void fillByToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.g_problemTypeTableAdapter.FillBy(this.guestDataSet.G_problemType);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            关于ToolStripMenuItem.Text = "正在清除，请稍等...";

            string exePath = System.Environment.CurrentDirectory + @"\html\";

            if (DeleteFiles(exePath))
            {
                MessageBox.Show("清除完成！");
            }

            关于ToolStripMenuItem.Text = "清空冗余文件";
        }

        public bool DeleteFiles(string path)
        {
            if (Directory.Exists(path) == false)
            {
                MessageBox.Show("没有这个目录!");
                return false;
            }
            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] files = dir.GetFiles();
            if (files.Length == 0)
            {
                MessageBox.Show("没有冗余文件!");
                return false;
            }
            try
            {
                foreach (var item in files)
                {
                    File.Delete(item.FullName);
                }
                return true;
            }
            catch (Exception)
            {

                MessageBox.Show("出错了!");
                return false;
            }
        }

        private void 删除类型ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (listBox1.SelectedItem != null)
            {
                DialogResult dr = MessageBox.Show("确认删除选中类型吗？", "提示", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    Mydb mydb = new Mydb();
                    string name = listBox1.SelectedValue.ToString();
                    mydb.deleteProblemType(name);
                    mydb.coleconn();
                    Sxcd();
                }

            }

        }

        private void 删除代码ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (listBox1.SelectedItem != null)
            {
                DialogResult dr = MessageBox.Show("确认删除选中代码吗？", "提示", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    Mydb mydb = new Mydb();
                    string name = listBox2.SelectedItem.ToString();
                    string[] nameStr = name.Split('—');

                    mydb.deleteProblem(nameStr[0]);
                    mydb.coleconn();
                    xuanz();
                }

            }
        }

        private void 查看ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem != null)
            {
                string name = listBox2.SelectedItem.ToString();
                string[] nameStr = name.Split('—');
                Form2 form2 = new Form2(nameStr[0]);
                form2.Text = nameStr[1];
                form2.Show();
            }
        }

        private void 纯文本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem != null)
            {
                string name = listBox2.SelectedItem.ToString();
                string[] nameStr = name.Split('—');
                Form5 form5 = new Form5(nameStr[0]);
                form5.Text = nameStr[1];
                form5.Show();
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem != null)
            {
                string name = listBox2.SelectedItem.ToString();
                string[] nameStr = name.Split('—');
                FormTextEdit Form3 = new FormTextEdit(nameStr[0]);
                Form3.ShowDialog();
                xuanz();
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (listBox1.SelectedItem != null)
            {
                DialogResult dr = MessageBox.Show("确认删除选中代码吗？", "提示", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    Mydb mydb = new Mydb();
                    string name = listBox2.SelectedItem.ToString();
                    string[] nameStr = name.Split('—');

                    mydb.deleteProblem(nameStr[0]);
                    mydb.coleconn();
                    xuanz();
                }

            }
        }

        private void 复制到剪切板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fuzhijqb();
            CopyMessage();
        }

        public void fuzhijqb()
        {
            if (listBox2.SelectedItem != null)
            {
                string name = listBox2.SelectedItem.ToString();
                string[] nameStr = name.Split('—');
                Form5 form5 = new Form5(nameStr[0]);

                Mydb mydb = new Mydb();
                string G_Fault = mydb.getG_Fault(nameStr[0]).Rows[0]["G_Fault"].ToString();

                G_Fault = Mydb.nachu(G_Fault);

                Clipboard.SetDataObject(G_Fault);
                mydb.coleconn();
            }
        }

        private void 代码ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            try
            {
                this.g_problemTypeTableAdapter.代码(this.guestDataSet.G_problemType);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private void 模板ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                this.g_problemTypeTableAdapter.模板(this.guestDataSet.G_problemType);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private void 代码获取器ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 迭代代码ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 代码获取器ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //Form6 Form6 = new Form6();
            //Form6.Show();
        }

        private void 迭代代码获取ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Form7 Form7 = new Form7();
            //Form7.Show();
        }

        private void 表Sql生成ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Form8 Form8 = new Form8();
            //Form8.Show();
        } 
        #endregion

        private Color RowBackColorAlt = Color.FromArgb(170, 201, 232);//交替色   
        private Color RowBackColorSel = Color.FromArgb(185, 209, 234);//选择项目颜色
        private Color wenzi = Color.Brown;//.FromArgb(220, 8, 8, 0);

        private void listBox2_DrawItem(object sender, DrawItemEventArgs e)
        {
            int index = e.Index;//获取当前要进行绘制的行的序号，从0开始。
            if (index == -1)
            {
                return;
            }
            Graphics g = e.Graphics;//获取Graphics对象。
            Rectangle bound = e.Bounds;//获取当前要绘制的行的一个矩形范围。

            string text = listBox2.Items[index].ToString();//获取当前要绘制的行的显示文本。

            //DataRowView rowView = listBox2.Items[i] as DataRowView;
            //string info = rowView["索引值OR列名"];

            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {   //如果当前行为选中行。
                //绘制选中时要显示的蓝色边框。

                Pen skyBluePen = new Pen(RowBackColorSel);
                skyBluePen.Width = 0.0F;
                //skyBluePen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;

                g.DrawRectangle(skyBluePen, bound.Left, bound.Top, bound.Width, bound.Height);
                Rectangle rect = new Rectangle(bound.Left, bound.Top, bound.Width, bound.Height);
                //绘制选中时要显示的蓝色背景。
                g.FillRectangle(Brushes.CornflowerBlue, rect);
                //绘制显示文本。
                Font font = new Font("微软雅黑", 12);
                TextRenderer.DrawText(g, text, font, rect, Color.White,
                                      TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
            }
            else
            {
                SolidBrush sb = null;
                if (e.Index % 2 == 0)
                {
                    sb = new SolidBrush(RowBackColorAlt);
                }
                else
                {
                    sb = new SolidBrush(RowBackColorSel);
                }
                using (Brush brush = sb)
                {
                    g.FillRectangle(brush, bound);//绘制背景色。
                }

                Font font = new Font("微软雅黑", 12);
                
                TextRenderer.DrawText(g, text, font, bound, wenzi,
                                      TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
            }
        }

        private void fillBy2ToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.g_problemTypeTableAdapter.FillBy2(this.guestDataSet.G_problemType);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void listBox2_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.C && e.Control)
            {

                e.Handled = true; //将Handled设置为true，指示已经处理过KeyPress事件   
                fuzhijqb();

                CopyMessage();
            }
        }

        public void CopyMessage() 
        {
            panel1.Visible = true;
            timer1.Interval = 800;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }

        private void 表Sql生成MySqlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //FormMysql FormMysql = new FormMysql();
            //FormMysql.Show();
        }

        private void 迭代代码生成ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Form9 Form9 = new Form9();
            //Form9.Show();
        }

        private void 格式化字符ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Form10 Form7 = new Form10();
            //Form7.Show();
        }

        private void 常用工具ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form11 Form11 = new Form11();
            Form11.Show();
        }

        private void 添加样式代码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3 Form3 = new Form3("");
            Form3.ShowDialog();
        }

        private void 修改样式代码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem != null)
            {
                string name = listBox2.SelectedItem.ToString();
                string[] nameStr = name.Split('—');
                Form3 Form3 = new Form3(nameStr[0]);
                Form3.ShowDialog();
                xuanz();
            }
        }

        private void 修改样式文本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem != null)
            {
                string name = listBox2.SelectedItem.ToString();
                string[] nameStr = name.Split('—');
                Form3 Form3 = new Form3(nameStr[0]);
                Form3.ShowDialog();
                xuanz();
            }
        }
    }
}
